package com.order.service;

import com.order.hit.crossover.platform.common.bean.order.Order;
import com.order.hit.crossover.platform.common.bean.order.OrderState;
import com.order.hit.crossover.platform.common.bean.order.Quotation;
import com.order.hit.crossover.platform.common.dao.QuotationDao;
import com.order.hit.crossover.platform.common.bean.order.OrderType;
import com.order.hit.crossover.platform.common.dao.OrderDao;
import com.order.hit.crossover.platform.common.utils.PFResponse;
import com.order.mapper.OrderMapper;
import com.order.utils.OverTimeJudge;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lei
 * @version 0.1
 * @date 2020/8/4
 */

@Component
@Qualifier("OrderServiceImpl")
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;


    @Autowired
    private OverTimeJudge overTimeJudge;

    @Override
    public void addOneOrder(OrderDao orderDao) {
        orderDao.setOrderState(OrderState.ORDER_DISPLAY.toString());
        this.orderMapper.insertoneOrder(orderDao);
    }


    @Override
    public List<Order> getALlorder(String storeId) {
        return OrderDao.getListOrder(this.orderMapper.getAllstoreId(storeId));
    }

    @Override
    public void deleteOrder(String orderId){
        OrderDao orderDao = this.orderMapper.getOrderDaoByid(orderId);
        if (orderDao.getOrderState().equals(OrderState.ORDER_SUCCESS)){
            LogManager.getLogger(OrderServiceImpl.class).info("no need to delete");
            return;
        }
        List<QuotationDao> list = QuotationDao.getListQuoDao((List<Quotation>)new RestTemplate().exchange(
                "http://quo:18085/quotationService/getQuotionByOrderId/"+orderId,
                HttpMethod.GET,
                new HttpEntity<>(new HttpHeaders()),
                PFResponse.class).getBody().get("data"));
        for (QuotationDao quotationDao : list){
           new RestTemplate().exchange("http://quo:18085/quotationService/deleteQuation/"+quotationDao.getQuotationId(), HttpMethod.DELETE, new HttpEntity<>(new HttpHeaders()), PFResponse.class);
        }
        this.orderMapper.deleteOrderId(orderId);
    }

    @Override
    public void updateOrderState(String orderId, String state) {
        OrderDao orderDao = this.orderMapper.getOrderDaoByid(orderId);
        orderDao.setOrderState(state);
        this.orderMapper.updateOrder(orderDao);
    }

    @Override
    public void successOrder(String orderId, String quoId) {
        this.updateOrderState(orderId, OrderState.ORDER_SUCCESS.toString());
    }

    @Override
    public List<Order> getAllOrder(String type) {
        overTimeJudge.judgeOrderIfOverTime();
        List<Order> result = new ArrayList<>();
        List<OrderDao> list = orderMapper.getAllOrderOfOneType(type.toUpperCase());
        for(OrderDao orderDao : list){
            result.add(OrderDao.getOrder(orderDao));
        }
        return result;
    }

    @Override
    public List<Order> getAllDisplayOrder(String type) {
        overTimeJudge.judgeOrderIfOverTime();
        List<Order> result = new ArrayList<>();
        List<OrderDao> list = orderMapper.getAllOrderOfOneType(type.toUpperCase());
        for(OrderDao orderDao : list){
            if(orderDao.getOrderState().equalsIgnoreCase(OrderState.ORDER_DISPLAY.toString())){
                result.add(OrderDao.getOrder(orderDao));
            }
        }
        return result;
    }

    @Override
    public Order getOrderByid(String id) {
        overTimeJudge.judgeOrderIfOverTime();
        return OrderDao.getOrder(this.orderMapper.getOrderDaoByid(id));
    }

    @Override
    public List<Order> getBuyOrderofstore(String storeId) {
        overTimeJudge.judgeOrderIfOverTime();
        List<OrderDao> orderDaoList = this.orderMapper.getAllstoreId(storeId);
        return getOrderByType(orderDaoList,OrderType.ORDER_BUY);
    }

    @Override
    public List<Order> getSoldOrderofstore(String storeId) {
        overTimeJudge.judgeOrderIfOverTime();
        List<OrderDao> orderDaoList = this.orderMapper.getAllstoreId(storeId);
        return getOrderByType(orderDaoList,OrderType.ORDER_SOLD);
    }

    /**
     * 根据订单类型 求购/求售，得到订单列表
     * @param list
     * @param orderType
     * @return
     */
    public List<Order> getOrderByType(List<OrderDao> list, OrderType orderType){
        overTimeJudge.judgeOrderIfOverTime();
        List<Order> result = new ArrayList<>();
        for(OrderDao orderDao:list){
            if(orderType.toString().equals(orderDao.getOrderType())){
                result.add(OrderDao.getOrder(orderDao));
            }
        }
        return result;
    }


}
